package com.ying.core.page;
import com.ying.core.exception.CustomException;
import com.ying.core.response.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页请求参数
 * @author 硬
 * @date 2021/12/04 20:13
 */
@ApiModel("分页请求对象")
public class PageWrap<T> {

    @ApiModelProperty("目标页")
    private Integer page;

    @ApiModelProperty("每页大小")
    private Integer capacity;

    @ApiModelProperty("排序字段")
    private Order order;

    @ApiModelProperty("模型")
    private T model;

    public boolean check() {
        return !(this.page == null | capacity == null | order == null | model == null);
    }

    public Sort sortMethod() {
        if (this.order == null) {
            throw new CustomException(ResultCode.PARAM_NOT_COMPLETE, "PageWrap.getSort");
        }
        if (order.getOrderMethod().equalsIgnoreCase("asc")) {
            return Sort.by(Sort.Direction.ASC,order.getOrderField());
        } else if (order.getOrderMethod().equalsIgnoreCase("desc")) {
            return Sort.by(Sort.Direction.DESC, order.getOrderField());
        }
        throw new CustomException(ResultCode.PARAM_IS_INVALID, "PageWrap.getSort");
    }

    public int getPage() {
        return page <= 0 ? 0 : page-1;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public int getCapacity () {
        return capacity <= 0 ? 10 : capacity;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public T getModel() {
        return model;
    }

    public void setModel(T model) {
        this.model = model;
    }
}
